﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.ObjectBuilder;
using GC2.Bussines.BMSeguridad;
using GC2.Entities.Seguridad;
using GC2.ExceptionHandler;
using GC2.Entities.Admin;
public partial class Admin_BuscarConcepto : GenericPage
{

    #region Propiedades
    Usuarios usuario = new Usuarios();
    private List<Conceptos> listaConceptos
    {
        get
        {
            return (List<Conceptos>)Session["listaConceptos"];
        }
        set
        {
            Session["listaConceptos"] = value;
        }
    }

    #endregion

    #region Controles

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SetearCultura();
            SetearConexion();
            registrarScript();
            if (!IsPostBack)
            {
                usuario = (Usuarios)Session["UsuarioActual"];
                string sConcepto = Session["Tipo"].ToString();
                string sTitulo = "Buscar " + sConcepto;
                lblTitulo.Text = sTitulo;
                string strOkEvent = ClientScript.GetPostBackEventReference(gvClientes, string.Empty);
                gvClientes.Attributes.Add("gvClientes_SelectedIndexChanged", strOkEvent);

                llenarControlesDeQueryString();
                List<Egresos> listaConceptos = new List<Egresos>();

                //llenar este objeto con los datos del cliente a filtrar
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion..

    #region btnBuscar_Click
    protected void btnBuscar_Click(object sender, EventArgs e)
    {
        try
        {
            gridDataBaund();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region gvClientes_PageIndexChanging
    protected void gvClientes_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            gvClientes.PageIndex = e.NewPageIndex;
            gridDataBaund();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region gvClientes_SelectedIndexChanged
    protected void gvClientes_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            // Evento manejado por Java.
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region gvClientes_RowDataBound
    protected void gvClientes_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
           
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion  

    #endregion

    #region Metodos

    #region llenarControlesDeQueryString
    private void llenarControlesDeQueryString()
    {
        try
        {
            if (Request.QueryString["Id_Concepto"] != null)
            {
                txtIdConcepto.Text = Request.QueryString["Id_Concepto"].ToString();
            }
            if (Request.QueryString["Concepto"] != null)
            {
                txtConcepto.Text = Request.QueryString["Concepto"].ToString();
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region registrarScript
    private void registrarScript()
    {
        try
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ClienteScript",
            @"
        <script language=""javascript"" type=""text/javascript"">
        <!--
        function returnvalue(IdConcepto, Concepto, CodEx)
        {
            var vIdConcepto = document.getElementById(IdConcepto);
            var vConcepto = document.getElementById(Concepto);
            var vCodEx = document.getElementById(CodEx);

            //para cuando el PopUP fue llamado de una pagina con MasterPage
            if(window.opener.aspnetForm != null)
            {
                if(window.opener.aspnetForm.ctl00_ContentPlaceHolder1_txtIdConcepto != null)
                {
                    window.opener.aspnetForm.ctl00_ContentPlaceHolder1_txtIdConcepto.value = vIdConcepto.innerHTML;
                }
                if(window.opener.aspnetForm.ctl00_ContentPlaceHolder1_txtConcepto != null)
                {
                    window.opener.aspnetForm.ctl00_ContentPlaceHolder1_txtConcepto.value = vConcepto.innerHTML;
                }
                if(window.opener.aspnetForm.ctl00_ContentPlaceHolder1_txtCodEx != null)
                {
                    window.opener.aspnetForm.ctl00_ContentPlaceHolder1_txtCodEx.value = vCodEx.innerHTML;
                }           
            }
            window.close();
        }
        //-->   
        </script>
        ");
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region gridDataBaund
    private void gridDataBaund()
    {
        try
        {
            listaConceptos = (List<Conceptos>)Session["listaConceptos"];
            
            Conceptos concepto = new Conceptos();
            bool bEncontrado = false;
            if (txtIdConcepto.Text != "")
            {
                concepto.Id_Concepto = Convert.ToInt16(txtIdConcepto.Text.Trim());
                foreach (Conceptos oConcepto in listaConceptos)
                {
                    if (oConcepto.Id_Concepto == concepto.Id_Concepto)
                    {
                        oConcepto.Concepto = concepto.Concepto;
                        bEncontrado = true;
                        break;
                    }
                }
            }
            //if (txtCodEx.Text != "")
            //{
            //    concepto.CodEx = txtCodEx.Text.Trim();
            //    foreach (Conceptos oConcepto in listaConceptos)
            //    {
            //        if (oConcepto.CodEx == concepto.CodEx)
            //        {
            //            oConcepto.Id_Concepto = concepto.Id_Concepto;
            //            oConcepto.CodEx = concepto.CodEx;
            //            bEncontrado = true;
            //            break;
            //        }
            //    }
            //}
            if (txtConcepto.Text != "")
            {
                concepto.Concepto = txtConcepto.Text.Trim();
                foreach (Conceptos oConcepto in listaConceptos)
                {
                    if (oConcepto.Concepto == concepto.Concepto)
                    {
                        oConcepto.Id_Concepto = concepto.Id_Concepto;
                        bEncontrado = true;
                        break;
                    }
                }
            }
            if (bEncontrado)
            {
                List<Conceptos> listTemp = new List<Conceptos>();
                listTemp.Add(concepto);
                gvClientes.DataSource = listTemp;
            }
            else
            {
                gvClientes.DataSource = listaConceptos;
            }
            gvClientes.DataBind();
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion  

    #endregion
}
